home *** CD-ROM | disk | FTP | other *** search
/ AppleScript - The Beta Release / AppleScript - The Beta Release.iso / Documentation / develop / Better Apple Event Coding / Code Samples / Document.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-16  |  7.6 KB  |  251 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------------------
  2.  
  3.     Program:    CPlusTESample 2.0
  4.     File:        Document.h
  5.     Uses:       Application.h
  6.                 List.h
  7.  
  8.     by Andrew Shebanow
  9.     of Apple Macintosh Developer Technical Support
  10.  
  11.     Copyright © 1989-1990 Apple Computer, Inc.
  12.     All rights reserved.
  13.  
  14. ------------------------------------------------------------------------------------------*/
  15.  
  16.  
  17. #ifndef __DOCUMENT__
  18. #define __DOCUMENT__
  19.  
  20. // Include necessary interface files
  21. #ifndef __TYPES__
  22. #include <Types.h>
  23. #endif
  24. #ifndef __QUICKDRAW__
  25. #include <QuickDraw.h>
  26. #endif
  27. #ifndef __WINDOWS__
  28. #include <Windows.h>
  29. #endif
  30. #ifndef __FILES__
  31. #include <Files.h>
  32. #endif
  33.  
  34. #ifndef __ALIASES__
  35. #include <Aliases.h>
  36. #endif
  37.  
  38. #ifndef __APPLICATION__
  39. #include "Application.h"
  40. #endif
  41. #ifndef __LIST__
  42. #include "List.h"
  43. #endif
  44.  
  45. // Define HiWord and LoWord inlines for efficiency.
  46. inline unsigned short HiWord(unsigned long val)
  47. {
  48.     return (unsigned short) ((val >> 16) & 0x0000FFFF);
  49. }
  50.  
  51. inline unsigned short LoWord(unsigned long val)
  52. {
  53.     return (unsigned short) (val & 0x0000FFFF);
  54. }
  55.  
  56. // max and min inlines
  57. inline max(long a, long b)
  58. {
  59.     return a > b ? a : b;
  60. }
  61.  
  62. inline min(long a, long b)
  63. {
  64.     return a < b ? a : b;
  65. }
  66.  
  67. // Define inlines to convert between Points & Rects for convenience.
  68. inline Point TopLeft(Rect r)
  69. {
  70.     Point pt;
  71.  
  72.     pt.v = r.top;
  73.     pt.h = r.left;
  74.     return pt;
  75. }
  76.  
  77. inline Point TopRight(Rect r)
  78. {
  79.     Point pt;
  80.  
  81.     pt.v = r.top;
  82.     pt.h = r.right;
  83.     return pt;
  84. }
  85.  
  86. inline Point BotLeft(Rect r)
  87. {
  88.     Point pt;
  89.  
  90.     pt.v = r.bottom;
  91.     pt.h = r.left;
  92.     return pt;
  93. }
  94.  
  95. inline Point BotRight(Rect r)
  96. {
  97.     Point pt;
  98.  
  99.     pt.v = r.bottom;
  100.     pt.h = r.right;
  101.     return pt;
  102. }
  103.  
  104. const long kMaxSleepTime = 60;    // 1 second worth of ticks
  105.  
  106. // we derive from handle object to prevent fragmentation
  107. class TDocument {
  108. protected:
  109.     WindowPtr            fDocWindow;
  110.     FSSpec                fFile;
  111.     short                fFileRefNum;
  112.     OSType                fFileType;
  113.     Boolean                fNewDoc;
  114.     Boolean                fReadOnly;
  115.     Boolean                fDirty;
  116.     Boolean                fActive;
  117.  
  118.     // internal file i/o routines - these are the guys that do the
  119.     // actual work of manipulating files
  120.     virtual void        OpenFile(Boolean readOnly, Boolean createIfNecessary);
  121.         // OpenFile opens the file referred to by fFile. If createIfNecessary
  122.         // is true and the file doesn't exist, it will be created.
  123.         // If readOnly is true, the fReadOnly field will be set correctly
  124.         // and the file will be opened with a read-only access path.
  125.         // You shouldn't need to override this routine.
  126.     virtual void        CloseFile();
  127.         // close file just closes the file specified by fFileRefNum
  128.         // You shouldn't need to override this routine.
  129.     virtual void        ReadFromFile(short refNum) = 0;
  130.         // this routine reads the file's data from the data fork
  131.         // specified by refNum. The file will already be open,
  132.         // and in the correct position for reading.
  133.         // You MUST override this routine.
  134.     virtual void        WriteToFile(short refNum) = 0;
  135.         // this routine writes the files data to the data fork
  136.         // specified by refNum. The file will already be open,
  137.         // and in the correct position for writing.
  138.         // You MUST override this routine.
  139.  
  140.     virtual YNCResult    PresentSaveDialog(Boolean quitting);
  141.         // this routine puts up the standard Save dialog,
  142.         // using the filename specified by fFile. The quitting
  143.         // parameter is used to make sure we have the right
  144.         // phrasing in the dialog box - either
  145.         // 'Save "x" before quitting?' or 'Save "x" before closing?'
  146.  
  147. public:
  148.     // Constructor & Destructor
  149.                         TDocument(short resID, OSType theFileType);
  150.         // creates new, untitled document using resID as window template
  151.     virtual                ~TDocument();
  152.         // our destructor - disposes of window
  153.  
  154.     // Routines to handle basic user interface events
  155.     // you MUST override these in your subclasses!!!
  156.     virtual void        DoZoom(short partCode) = 0;
  157.     virtual void        DoGrow(EventRecord* theEvent) = 0;
  158.     virtual void        DoContent(EventRecord* theEvent) = 0;
  159.     virtual void        DoKeyDown(EventRecord* theEvent) = 0;
  160.     virtual void        DoUpdate() = 0;
  161.  
  162.     // called when activating/deactivating document.
  163.     // default version just sets fActive variable, so
  164.     // you will need to override this to hilite your selection
  165.     // and such
  166.     virtual void        DoActivate(Boolean becomingActive);
  167.  
  168.     // these routines are called after a TDocument object has been
  169.     // created to initialize its contents. You probably won't need to
  170.     // override these, since they are fairly generic.
  171.     virtual void        OpenNewDoc() {};
  172.         // OpenNewDoc sets up a new, untitled document - by default, this
  173.         // has already been done in the TDocument constructor, so this routine
  174.         // is just a placeholder.
  175.     virtual void        OpenOldDoc(FSSpec theFile, Boolean readOnly);
  176.         // OpenOldDoc opens a document, reads its contents, sets the window
  177.         // title, and so on.
  178.  
  179.     // high level, user-oriented file handling routines
  180.     // You probably won't need to override these
  181.     virtual YNCResult    DoClose(Boolean askUserToSave,
  182.                                 YNCResult defaultAnswer,
  183.                                 Boolean quitting);
  184.         // DoClose closes the document. If askUserToSave is true,
  185.         // the user is asked whether or not he wants to save the documents
  186.         // contents. If it is false, the document will be saved if defaultAnswer
  187.         // is yesResult.
  188.     virtual void        DoSave();
  189.         // DoSave saves the documents contents. If the document is a new,
  190.         // untitled document, DoSave will call DoSaveAs (below). Otherwise,
  191.         // it sets up the file for writing and calls WriteToFile to save the
  192.         // actual data.
  193.     virtual void        DoSaveAs();
  194.         // DoSaveAs asks the user for a file to save the document into.
  195.         // It saves the data in the same manner as DoSave, and sets up
  196.         // the file-related data members (fFile, fFileRefNum, etc).
  197.     virtual void        DoRevert() {};
  198.         // DoRevert is a hook for a revert routine. Currently, this
  199.         // isn't implemented, but it is here for future enhancement.
  200.     virtual void        DoPrint() {};
  201.         // we don't support printing yet either
  202.  
  203.     // standard edit menu actions
  204.     // with the exception of DoUndo, you MUST override these routines
  205.     virtual void        DoUndo() {};
  206.         // by default, undo is unimplemented, so you don't have to
  207.         // override this method
  208.     virtual void        DoCut() = 0;
  209.     virtual void        DoCopy() = 0;
  210.     virtual void        DoPaste() = 0;
  211.     virtual void        DoClear() = 0;
  212.     virtual void        DoSelectAll() = 0;
  213.  
  214.     // idle time routines: you can override these to do cursor handling,
  215.     // TE caret blinking, marquee effects, etc...
  216.     virtual void        DoIdle() {};
  217.     virtual unsigned long CalcIdle() { return kMaxSleepTime; };
  218.         // we never need idle in typical applications, so return a very large number
  219.     virtual void        AdjustCursor(Point /* where */) {};
  220.         // where is in local coords
  221.  
  222.     // query state of document - useful for adjusting menu state
  223.     // You will probably need to override at least a few of
  224.     // these to accurately reflect the state of your document
  225.     virtual Boolean        HaveUndo() { return false; };
  226.     virtual Boolean        HaveSelection() { return false; };
  227.     virtual Boolean        HavePaste() { return false; };
  228.     virtual Boolean        CanClose() { return (FrontWindow() != nil); };
  229.     virtual Boolean        CanSave() { return fDirty; };
  230.     virtual Boolean        CanSaveAs() { return true; };
  231.     virtual Boolean        CanRevert() { return false; };    // not implemented
  232.     virtual Boolean        CanPrint() { return false; };    // not implemented
  233.  
  234.     // utility routine to get window pointer for document
  235.     inline WindowPtr    GetDocWindow() { return fDocWindow; }
  236. };
  237.  
  238. // TDocumentList is a simple linked list of documents,
  239. // implemented C++ style.
  240. class TDocumentList : public TList {
  241. public:
  242.     virtual void        AddDoc(TDocument* doc);
  243.     virtual void        RemoveDoc(TDocument* doc);
  244.     // find the TDocument associated with the window
  245.     virtual TDocument*    FindDoc(WindowPtr window);
  246.     // return number of active documents
  247.     inline int            NumDocs() { return Count(); }
  248. };
  249.  
  250. #endif
  251.